package com.jhws.business.comm.service;

import com.jhws.business.comm.bo.RoomTypeGroup;
import com.jhws.core.db.BaseDao;
import com.jhws.util.StringUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class RoomTypeGroupService {
    @Resource
    private BaseDao BaseDao;

    /**
     * 保存户型组信息
     *
     * @param roomTypeGroup 户型组信息
     */
    public void saveRoomTypeGroup(RoomTypeGroup roomTypeGroup) {
        if (roomTypeGroup.getCreationTime() == null) {
            roomTypeGroup.setCreationTime(new Date());
        }
        BaseDao.saveOrUpdate(roomTypeGroup);
    }

    /**
     * 查询户型组信息
     *
     * @param id 户型组信息ID
     */
    public RoomTypeGroup queryRoomTypeGroup(long id) {
        return BaseDao.get(RoomTypeGroup.class, id);
    }

    /**
     * 条件查询户型组信息列表
     *
     * @param where 条件
     */
    public List<RoomTypeGroup> queryRoomTypeGroups(String where) {
        return BaseDao.findList("from RoomTypeGroup " + where);
    }

    /**
     * 分页查询户型组信息列表
     *
     * @param param 条件
     */
    public Map queryRoomTypeGroupPage(Map param) {
        String hql = "from RoomTypeGroup where 1=1 ";
        String commCode = MapUtils.getString(param, "commCode");
        if (StringUtils.isNotEmpty(commCode)) {
            hql += "and comm_code='" + commCode + "' ";
        }
        String groupName = MapUtils.getString(param, "groupName");
        if (StringUtils.isNotEmpty(groupName)) {
            hql += "and group_name like '%" + groupName + "%' ";
        }
        return BaseDao.queryPage(hql);
    }

    /**
     * 删除户型信息
     *
     * @param roomTypeGroup 户型组信息ID
     */
    public void deleteRoomTypeGroup(RoomTypeGroup roomTypeGroup) {
        BaseDao.h().delete(roomTypeGroup);
    }

    /**
     * 条件删除户型组信息
     *
     * @param where 条件
     */
    public void deleteRoomTypeGroup(String where) throws Exception {
        String sql = "delete from room_type_group " + where;
        BaseDao.executeBySql(sql);
    }
}
